package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.common.PageResult;
import com.heima.mapper.RegionMapper;
import com.heima.pojo.Region;
import com.heima.pojo.dto.PageQueryDTO;
import com.heima.pojo.dto.RegionSaveDTO;
import com.heima.pojo.vo.RegionVo;
import com.heima.service.RegionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RegionServiceImpl implements RegionService {

    @Autowired
    private RegionMapper regionMapper;


    @Override
    public PageResult queryRegion(PageQueryDTO dto) {
        PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
        Page<RegionVo> page = regionMapper.queryRegion(dto);
        return new PageResult(dto.getPageIndex(), dto.getPageSize(), page.getPages(), page.getTotal(),page.getResult());
    }

    @Override
    public Boolean save(RegionSaveDTO dto) {
        Region region = new Region();
        region.setName(dto.getRegionName());
        region.setRemark(dto.getRemark());
        int result = regionMapper.insert(region);
        return result != 0;
    }

    @Override
    public Boolean update(String regionId, RegionSaveDTO dto) {
        Region region = new Region();
        region.setId(Long.parseLong(regionId));
        region.setName(dto.getRegionName());
        region.setRemark(dto.getRemark());
        int result = regionMapper.updateById(region);
        return result != 0;
    }

    @Override
    public Boolean delete(String regionId) {
        int result = regionMapper.deleteById(regionId);
        return result != 0;
    }
}
